﻿using OrderServicePlus;

namespace OrderManagement
{
    internal class Program
    {
        static void Main(string[] args)
        {
            OrderService service = new OrderService();

            while (true)
            {
                Console.WriteLine("请输入命令：");
                string command = Console.ReadLine();
                string[] parameters = command.Split(' ');
                try
                {
                    switch (parameters[0])
                    {
                        case "help":
                            Console.WriteLine("使用指导：");
                            Console.WriteLine("可用命令：");
                            Console.WriteLine("add（添加订单）：输入：ID 客户名 商品名 数量 单价");
                            Console.WriteLine("remove（删除订单）：输入：ID");
                            Console.WriteLine("modify（修改订单）：输入：ID 客户名 商品名 数量 单价");
                            Console.WriteLine("query（查询订单），输入：查询类型 + 选择类型对应的变量");
                            Console.WriteLine("包含：byOrderId（ID） byProductName（商品名） byCustomer（客户名） byAmount（订单金额）");
                            Console.WriteLine("export（导出订单）");
                            Console.WriteLine("import（导入订单）");
                            Console.WriteLine("quit（退出程序）");
                            break;

                        case "add":
                            Order order = new Order
                            {
                                OrderId = parameters[1],
                                Customer = parameters[2],
                                Details = new List<OrderDetail>()
                            };
                            for (int i = 3; i < parameters.Length; i += 4)
                            {
                                OrderDetail detail = new OrderDetail
                                {
                                    ProductName = parameters[i],
                                    Quantity = int.Parse(parameters[i + 1]),
                                    UnitPrice = double.Parse(parameters[i + 2])
                                };
                                order.Details.Add(detail);
                            }
                            service.AddOrder(order);
                            break;

                        case "remove":
                            service.RemoveOrder(parameters[1]);
                            break;

                        case "modify":
                            Order newOrder = new Order
                            {
                                OrderId = parameters[1],
                                Customer = parameters[2],
                                Details = new List<OrderDetail>()
                            };
                            for (int i = 3; i < parameters.Length; i += 4)
                            {
                                OrderDetail detail = new OrderDetail
                                {
                                    ProductName = parameters[i],
                                    Quantity = int.Parse(parameters[i + 1]),
                                    UnitPrice = double.Parse(parameters[i + 2])
                                };
                                newOrder.Details.Add(detail);
                            }
                            service.ModifyOrder(newOrder);
                            break;

                        case "query":
                            switch (parameters[1])
                            {
                                case "byOrderId":
                                    var orders1 = service.QueryOrdersByOrderId(parameters[2]);
                                    foreach (var o in orders1)
                                    {
                                        Console.WriteLine(o);
                                    }
                                    break;
                                case "byProductName":
                                    var orders2 = service.QueryOrdersByProductName(parameters[2]);
                                    foreach (var o in orders2)
                                    {
                                        Console.WriteLine(o);
                                    }
                                    break;
                                case "byCustomer":
                                    var orders3 = service.QueryOrdersByCustomer(parameters[2]);
                                    foreach (var o in orders3)
                                    {
                                        Console.WriteLine(o);
                                    }
                                    break;
                                case "byAmount":
                                    var orders4 = service.QueryOrdersByAmount(double.Parse(parameters[2]));
                                    foreach (var o in orders4)
                                    {
                                        Console.WriteLine(o);
                                    }
                                    break;
                                default:
                                    Console.WriteLine("无效的查询类型。");
                                    break;
                            }
                            break;

                        case "sort":
                            service.Sort();
                            break;

                        case "quit":
                            return;

                        case "export":
                            service.Export("orders.xml");
                            break;

                        case "import":
                            service.Import("orders.xml");
                            break;

                        default:
                            Console.WriteLine("无效的命令。");
                            break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

        }
    }
}